﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_CustomerDetail : AdminPageBase
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUrlArgs();
        GetUserPermisions();
        if (!IsPostBack)
        {
            BindCenter();
            BindCustomer();
            BindCustomerEmp();
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void BindCenter()
    {
        DataSet ds = jtDataAccess.GetList("[Center]", "1=1", "CenterId");
        if (CoreTools.CheckDataSet(ds))
        {

            ddlCenter.DataSource = ds;
            ddlCenter.DataTextField = "Name";
            ddlCenter.DataValueField = "CenterId";
            ddlCenter.DataBind();

        }
        ListItem item = new ListItem("=请选择所属指挥中心=", "0");
        ddlCenter.Items.Insert(0, item);
    }
    protected void BindCustomer()
    {
        if (id != 0)
        {
            DataSet ds = new DataSet();
            string strwhere = "CustomerId="+id;
            ds = jtDataAccess.GetList("Customer", strwhere, "CustomerId");
            if (CoreTools.CheckDataSet(ds))
            {
                txtname.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                txtcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                ddlCenter.SelectedValue = ds.Tables[0].Rows[0]["CenterId"].ToString();
              
            }
           

        }

    }
    protected void GetUrlArgs()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;
          
        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);

        }
    }
    protected void BindCustomerEmp()
    {
        DataSet ds = jtDataAccess.SearchEmpByCustomerId("", id, 10, 0, JTDataAccess.EmpSortField.Name, JTDataAccess.SortType.DESC);
        if (CoreTools.CheckDataSet(ds))
        {

            grdvEemp.DataSource = ds;
            grdvEemp.DataBind();
        }
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("CustomerDetail.aspx?MenuId=" + MenuId);
    }
    protected void grdvEemp_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdvEemp_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
           
        }
        
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string name = txtname.Text.Trim();
        string code = txtcode.Text.Trim();
        int centerid = int.Parse(ddlCenter.SelectedValue.ToString());
        UserType ut = new UserType();//用户自定义类型
        DataTable CustomerTable = ut.GetCustomerTableType();
        DataRow CustomerRow;
        JTDataAccess.OpType OpType;
        if (id == 0)
        {
            OpType = JTDataAccess.OpType.Insert;
           
        }
        else
        {
            OpType = JTDataAccess.OpType.Update;
           
        }
        CustomerRow = CustomerTable.NewRow();
        CustomerRow["CENTERID"] = Convert.ToInt32(ddlCenter.SelectedValue.ToString());
        CustomerRow["NAME"] = name;
        CustomerRow["CODE"] = code;
        CustomerTable.Rows.Add(CustomerRow);
        CustomerTable.AcceptChanges();
        int flag = jtDataAccess.OpCustomer(OpType, id, code, name, centerid);

        switch (flag)
        {
            case -1:
                JsHelper.Alert("操作失败，该客户名称已经存在！");
                break;
            case 0:
                JsHelper.Alert("操作失败！");
                break;
            default:
                JsHelper.Alert("操作成功");
                break;
        }
    }
}